<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/23
 * Time: 21:07
 */

namespace App\Http\Controllers;


use App\Comment;
use App\User;

class CommentController extends Controller
{
    function send(){
        date_default_timezone_set('Asia/Shanghai');
        $rating=$_POST['rating'];
        switch ($rating){
            case '简直可怕':
                $score=2;
                break;
            case '真的难看':
                $score=4;
                break;
            case '还是可以':
                $score=6;
                break;
            case '超级好看':
                $score=8;
                break;
            case '五星神作':
                $score=10;
                break;
        }
        $data=Comment::create([
            'film_id'=>$_POST['film_id'],
            'content'=>$_POST['content'],
            'rating'=>$score,
            'assist_num'=>$_POST['assist_num'],
            'user_name'=>$_POST['user_name'],
            'time'=>date('Y-m-d H:i:s',time())
        ]);
        if($data){
            return 'right';
        }
        else{
            return 'wrong';
        }
    }
    function assist(){
        $id=$_POST['id'];//评论id
        $assist=$_POST['assist'];//点赞用户名
        $comment=Comment::find($id);
        $user=User::where('nickname','=',$assist)->get();
        $user_id=$user[0]['id'];
        if($comment['assist_user']==""){
            $comment['assist_user']=$user_id;
            $comment['assist_num']=1;
            $comment->save();
            return 'right';
        }
        else{
            $arr=explode(",",$comment['assist_user']);
            foreach ($arr as $item){
                if($item==$user_id){
                    return "你赞过这条评论了";
                }
            }
            $comment['assist_user']=$comment['assist_user'].','.$user_id;
            $comment['assist_num']+=1;
            $comment->save();
            return 'right';
        }
    }
    function delete(){
        $id=$_POST['id'];
        $comment=Comment::find($id);
        $res=$comment->delete();
        if ($res){
            return 'success';
        }
        else{
            return '评论删除失败';
        }
    }
}